<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.guanjianzi" placeholder="请输入查询条件"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getemployees">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>
    <!--
    //字段-部门编号
    private String sn;
    //字段-名字
    private String name;
    private String state;
    //与员工表关联起来
    private Employee manager;
    //自关联查询
    private Department parent;
    -->
		<!--列表-->

		<el-table :data="employees" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="username" label="姓名" width="180" sortable>
			</el-table-column>
			<el-table-column prop="email" label="邮箱" width="180" sortable>
			</el-table-column>
			<el-table-column prop="phone" label="电话号码" width="180" sortable>
			</el-table-column>
			<el-table-column prop="password" label="密码" width="180" sortable>
			</el-table-column>
			<el-table-column prop="age" label="年龄" width="180" sortable>
			</el-table-column>
			<el-table-column prop="state" label="启用状态" width="200" :formatter="formatstate" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.state==0" style="color:green" >启用</span>
          <span v-else-if="scope.row.state==-1" style="color:red" >禁用</span>
          <span v-else style="color: yellow">未知</span>
        </template>
      </el-table-column>
<!--			<el-table-column prop="manager.username" label="管理员" width="180" sortable>
			</el-table-column>
			<el-table-column prop="parent.name" label="上级部门" width="200" sortable>
			</el-table-column>-->
			<el-table-column label="操作" width="150">
        <template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="total":current-page="currentPage"
                     style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
    <!--让编辑和新增共用一个弹出框,title字段要灵活-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="姓名" prop="username">
					<el-input v-model="editForm.username" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="邮箱" prop="email">
					<el-input v-model="editForm.email" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="电话号码" prop="phone">
					<el-input v-model="editForm.phone" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="密码" prop="password">
					<el-input v-model="editForm.password" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="年龄" prop="age">
					<el-input v-model="editForm.age" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="editForm.state">
						<el-radio class="radio" :label="0">启用</el-radio>
						<el-radio class="radio" :label="-1">禁用</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getDepartmentListPage, removeDepartment, batchRemoveDepartment, editDepartment, addDepartment } from '../../api/api';
//数据层
	export default {
    data() {
      return {
        title: "",
        filters: {
          guanjianzi: ''
        },
        employees:[],
        total: 0,
        currentPage: 1,
        pageSize: 10,
        listLoading: false,
        sels: [],//列表选中列

        editFormVisible: false,//编辑界面是否显示
        editLoading: false,
        editFormRules: {
          username: [
            {required: true, message: '请输入姓名', trigger: 'blur'}
          ],
          email: [
            {required: true, message: '请输入邮箱', trigger: 'blur'}
          ]
        },
        //编辑界面数据
        editForm: {
          id: null,
          username: '',
          email: '',
          phone: '',
          password: '',
          age:1,
          state: 0,
        },
      }
    },
    methods: {
      getemployees() {
        this.currentPage = 1;
        this.getEmployees();
      },
      handleCurrentChange(val) {
        this.currentPage = val;
        this.getEmployees();
      },
      //获取员工列表
      getEmployees() {
        let para = {//请求参数
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          guanjianzi: this.filters.guanjianzi//关键字查询--高级查询
        };
        //开启忙等
        this.listLoading = true;
        //NProgress.start();
        //调用后端接口获取数据库数据  url填后端控制层所指定的路径 后面跟着数据源
        this.$http.post("/employee", para)
            .then(result => {
              this.listLoading = false;
              result = result.data;
              this.employees = result.rows;
              this.total = result.total;
            })
            .catch(result => {
              console.log(result);
            })
      },
      //删除
      handleDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {//执行的逻辑
          this.listLoading = true;//开启忙等款
          //NProgress.start();
          let para = {id: row.id};
          this.$http.delete("/employee/" + row.id)//使用后端Restful规范的接口
              .then(result => {
                this.listLoading = false;//关闭忙等款
                result = result.data;
                if (result.success) {
                  this.$message({
                    message: result.message,
                    type: "success"
                  });
                } else {
                  this.$message({
                    message: result.message,
                    type: "error"
                  });
                }
                this.currentPage = 1;
                this.getEmployees();
              })
              .catch(result => {//这个是调用后端不通,后端有问题就报这个错误
                this.$message({
                  message: '服务繁忙!!请稍后重试',
                  type: "error"
                });
              })
        }).catch(() => {

        });
      },
      //显示编辑界面
      handleEdit: function (index, row) {
        this.editForm = Object.assign({}, row);//渲染表单
        this.title = "编辑";
        this.getEmployees();
        this.editFormVisible = true;//控制显示弹出框
        //this.$refs.editForm.resetFields();//重置表单校验

      },
      //显示新增界面
      handleAdd: function () {
        this.getEmployees();
        this.editFormVisible = true;//控制显示弹出框
        this.title = "新增";
        this.editForm = {
          id: null,
          username: '',
          email: '',
          phone: '',
          password: '',
          age:1,
          state: 0,
        };
        this.$refs.editForm.resetFields();//重置表单校验
      },
      //编辑和新增
      editSubmit: function () {
        this.$refs.editForm.validate((valid) => {//找到editForm表单,主动开启校验
          if (valid) {//校验通过
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.editLoading = true;//开启忙等框

              //NProgress.start();
              let para = Object.assign({}, this.editForm);
              this.$http.put("/employee", para)
                  .then(result => {
                    this.$refs['editForm'].resetFields();//重置表单校验
                    this.editLoading = false;//关闭按钮忙等
                    this.editFormVisible = false;//关闭弹出框
                    result = result.data;
                    if (result.success) {//后端修改或添加成功
                      this.$message({
                        message: result.message,
                        type: 'success'
                      });
                    } else {//后端修改或添加失败
                      this.$message({
                        message: result.message,
                        type: 'error'
                      });
                    }
                    this.currentPage = 1;
                    this.getEmployees();//重新刷新页面
                  })
                  .catch(result => {//调用后端调用不同
                    this.editLoading = false;//关闭按钮忙等
                    this.$message({
                      message: '系统繁忙，稍后重试',
                      type: 'error'
                    });
                    this.$refs['editForm'].resetFields();//重置表单校验
                  })
            });
          }
        });
      },
      selsChange: function (sels) {
        this.sels = sels;
      },
      //批量删除
      batchRemove: function () {
        //this.sels 当前选中的所有行的数据
        var ids = this.sels.map(item => item.id)
        this.$confirm('确认删除选中记录吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          //NProgress.start();
          //let para = { ids: ids };
          this.$http.patch("/employee", ids)
              .then(result => {
                //关闭忙等框
                this.listLoading = false;
                result = result.data;
                if (result.success) {
                  console.log(result);
                  this.$message({
                    message: result.message,
                    type: "success"
                  });
                } else {
                  this.$message({
                    message: result.message,
                    type: "error"
                  });
                }
                this.currentPage = 1;
                this.getEmployees();
              })
              .catch(result => {
                this.$message({
                  message: "系统繁忙,请稍后重试!!!",
                  type: "error"
                });
              })
        }).catch(() => {

        });
      },
      mounted() {
        this.getEmployees();
      }
    }
  }

</script>

<style scoped>

</style>